package com.cnswhy.cloud.server.app.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cnswhy.cloud.common.redis.service.RedisService;
import com.cnswhy.cloud.server.app.controller.BaseController;
import com.cnswhy.cloud.server.core.constant.RedisConstant;
import com.cnswhy.cloud.server.core.dto.ProductAppDetailVo;
import com.cnswhy.cloud.server.core.dto.ProductQueryBo;
import com.cnswhy.cloud.server.core.entity.ShopClassify;
import com.cnswhy.cloud.server.core.query.QueryGenerator;
import com.cnswhy.cloud.server.core.service.ShopClassifyService;
import com.cnswhy.cloud.server.core.service.shop.ShopProductService;
import com.cnswhy.cloud.server.core.vo.PagerVo;
import com.cnswhy.cloud.server.core.vo.RespVo;
import com.cnswhy.cloud.server.core.vo.shop.ProductAppPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "商品控制层")
@Controller
@RequestMapping("/shop/product")
@Slf4j
public class ProductController extends BaseController {


    @Autowired
    private ShopProductService shopProductService;


    @Autowired
    private ShopClassifyService shopClassifyService;
    @Autowired
    RedisService redisService;

    @ApiOperation(value = "分页查询商品信息", httpMethod = "POST")
    @ApiParam(name = "bo", value = "商品BO对象")
    @RequestMapping("/ajaxPageData")
    @ResponseBody
    public RespVo ajaxPageData(ProductQueryBo bo) {
        RespVo vo = new RespVo();
        try {

            PagerVo<ProductAppPageVo> queryPageData = shopProductService.queryProductAppPage(bo);


            vo.setCode(RespVo.CODE_SUCCESS);
            vo.setResult(queryPageData);
        } catch (Exception e) {
            vo.setCode(RespVo.CODE_ERROR);
            vo.setMessage("分页查询商品信息失败");
            log.error("分页查询商品信息失败", e);
        }
        return vo;
    }


    /**
     * 描述：查看商品详情
     * 作者：张润泽
     * 时间：2019年8月6日 下午6:50:20
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "商品详情", httpMethod = "POST")
    @ApiParam(name = "id", value = "商品ID")
    @RequestMapping("/queryProduct")
    @ResponseBody
    public RespVo queryProduct(Long id, HttpServletRequest request) {
        RespVo respVo = new RespVo();
        try {
            if (id == null) {
                respVo.setCode(RespVo.CODE_ERROR);
                respVo.setMessage("参数异常");
                return respVo;
            }
            ProductAppDetailVo queryProductAppById = shopProductService.queryProductAppById(id);

            respVo.setCode(RespVo.CODE_SUCCESS);
            respVo.setMessage("查询成功");
            respVo.setResult(queryProductAppById);
            respVo.setLogMsg(id + "");
        } catch (Exception e) {
            respVo.setCode(RespVo.CODE_ERROR);
            respVo.setMessage("查询失败");
            log.error("查询商品详情异常：", e);
        }
        return respVo;
    }

    /**
     * 描述：查询商品分类
     * 作者：钟斌
     * 时间：2020年04月09日 下午6:50:20
     *
     * @return
     */
    @ApiOperation(value = "查询商品分类", httpMethod = "POST")
    @RequestMapping("/queryProductTypeList")
    @ResponseBody
    public RespVo queryProductTypeList(HttpServletRequest request) {
        RespVo respVo = new RespVo();
        try {
            ShopClassify bo = new ShopClassify();
            bo.setParentId(-1L);
            QueryWrapper<ShopClassify> queryWrapper = QueryGenerator.initQueryWrapper(bo);
            queryWrapper.orderByAsc("sort_order");
            List<ShopClassify> queryByParams = shopClassifyService.list(queryWrapper);
            String pfx = String.valueOf(redisService.hget(RedisConstant.SYS_CONFIG_1, RedisConstant.FTP_PATH_PREFIX));
            for(ShopClassify shopClassify : queryByParams){
                if(StringUtils.isNotBlank(shopClassify.getUrl())){
                    shopClassify.setImgUrl(shopClassify.getUrl());
                }else{
                    shopClassify.setImgUrl(pfx + shopClassify.getImgUrl());
                }
            }
            respVo.setCode(RespVo.CODE_SUCCESS);
            respVo.setMessage("查询成功");
            respVo.setResult(queryByParams);
        } catch (Exception e) {
            respVo.setCode(RespVo.CODE_ERROR);
            respVo.setMessage("查询失败");
            log.error("查询商品详情异常：", e);
        }
        return respVo;
    }

    /**
     * 描述：查看商品赠品列表
     * 作者：张润泽
     * 时间：2019年8月6日 下午6:50:20
     * @param id
     * @return
     */
//    @RequestMapping("/queryGiftActivityList")
//    @ResponseBody
//    public RespVo queryGiftActivityList(ShopGiftActivityBo bo, HttpServletRequest request)
//    {
//        RespVo respVo = new RespVo();
//        try
//        {
//            if (bo.getShopId() == null)
//            {
//                respVo.setCode(RespVo.VALIDATE_ERROR);
//                respVo.setMessage("参数异常");
//                return respVo;
//            }
//            bo.setBeginDate(DateUtil.getCurrentDate(DateUtil.FORMAT1));
//            respVo.setCode(RespVo.CODE_SUCCESS);
//            respVo.setMessage("查询成功");
//            respVo.setResult(shopProductService.queryGiftActivityList(bo));
//        }
//        catch (Exception e)
//        {
//            respVo.setCode(RespVo.CODE_ERROR);
//            respVo.setMessage("查询失败");
//            log.error("查询商品赠送活动列表异常：", e);
//        }
//        return respVo;
//    }

    /**
     * 描述：查看商品赠品详情
     * 作者：张润泽
     * 时间：2019年8月6日 下午6:50:20
     * @param id
     * @return
     */
//    @RequestMapping("/queryGiftActivityById")
//    @ResponseBody
//    public RespVo queryGiftActivityById(Long id, HttpServletRequest request)
//    {
//        RespVo respVo = new RespVo();
//        try
//        {
//            if (id == null)
//            {
//                respVo.setCode(RespVo.VALIDATE_ERROR);
//                respVo.setMessage("参数异常");
//                return respVo;
//            }
//            respVo.setCode(RespVo.CODE_SUCCESS);
//            respVo.setMessage("查询成功");
//            respVo.setResult(shopProductService.queryGiftActivityById(id));
//        }
//        catch (Exception e)
//        {
//            respVo.setCode(RespVo.CODE_ERROR);
//            respVo.setMessage("查询失败");
//            log.error("查询商品赠送活动详情异常：", e);
//        }
//        return respVo;
//    }
}
